<template>
  <el-dialog
    :title="!dataForm.id ? $t('Common.Add') : $t('Common.Edit')"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="auto"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="dataForm.type" placeholder="请选择">
          <el-option label="酒店" :value="1"></el-option>
          <el-option label="房间" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="图标" prop="icon">

        <el-popover
          ref="iconListPopover"
          placement="bottom-start"
          trigger="click"
          popper-class="mod-menu__icon-popover"
        >
          <div class="mod-menu__icon-list">
            <el-button
              style="margin-left: 5px; margin-bottom: 5px"
              v-for="(item, index) in iconList"
              :key="index"
              @click="iconActiveHandle(item)"
              :class="{ 'is-active': item === dataForm.icon }"
            >
              <span :class="'hotel-icon '+ item"></span>
            </el-button>
          </div>
          <el-button
            slot="reference"
          >
            <span :class="'hotel-icon '+ dataForm.icon"></span>
          </el-button>
        </el-popover>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('Common.Cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ $t('Common.Confirm') }}</el-button>
    </span>
  </el-dialog>
</template>
<style>
  .mod-menu__icon-list{
    width: 500px;
  }

</style>
<script>
  export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: "",
        type: "",
        icon: ""
      },
      iconList:new Array(37).fill('').map((f,i) => "icon-icon-test" + (i === 0 ? "" : i)),
      svgList:[],
      dataRule: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        type: [
          { required: true, message: "1-酒店，2-房间不能为空", trigger: "blur" }
        ],
        icon: [{ required: true, message: "图标不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/hotel/hotelfacility/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.hotelFacility.name;
              this.dataForm.type = data.hotelFacility.type;
              this.dataForm.createTime = data.hotelFacility.createTime;
              this.dataForm.icon = data.hotelFacility.icon;
            }
          });
        }
      });
    },
    iconActiveHandle(item) {
      this.dataForm.icon = item;
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/hotel/hotelfacility/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              name: this.dataForm.name,
              type: this.dataForm.type,
              createTime: this.dataForm.createTime,
              icon: this.dataForm.icon
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
